package com.radio;

import com.radio.helper.FileHelper;
import com.radio.service.PlayerService;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity implements OnClickListener, OnSeekBarChangeListener{
	private AudioManager audioManager;
	private int currentVolume, maxVolume;
	private boolean isMutePressed, isRecordPressed, isMixerPressed;
	private ImageButton muteBtn, recordBtn, mixerBtn;
	private SeekBar volumeControl;
	private FileHelper fHelper;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initViews();
		startPlayerService();
	}
//----------------------------------------------------------------------------
	private void initViews(){
		audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
		
		//mute button
		muteBtn = (ImageButton)findViewById(R.id.button_mute);
	    muteBtn.setOnClickListener(this);
	    
	    //record button
		recordBtn = (ImageButton)findViewById(R.id.button_record);
	    recordBtn.setOnClickListener(this);
	    
	    //mixer button
		mixerBtn = (ImageButton)findViewById(R.id.button_mixer);
	    mixerBtn.setOnClickListener(this);
	    
	    //volume bar
		volumeControl = (SeekBar)findViewById(R.id.volume_bar);
		volumeControl.setOnSeekBarChangeListener(this);
	    
		maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	    currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
	    volumeControl.setMax(maxVolume);
	    volumeControl.setProgress(currentVolume);
	    
	    //object for recording music
	    fHelper = new FileHelper();
	}
//----------------------------------------------------------------------------
	/**
	 * Buttons click listener
	 */
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		
		//mute button
		case R.id.button_mute: {
			isMutePressed = !isMutePressed;
			if (isMutePressed){
				muteBtn.setBackgroundResource(R.drawable.button_pressed);
				muteBtn.setImageResource(R.drawable.mute_on);
				currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
			}else{
				muteBtn.setBackgroundResource(R.drawable.button_default);
				muteBtn.setImageResource(R.drawable.mute_off);
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
			}
			break;
		}
		
		//record button
		case R.id.button_record: {
			isRecordPressed = !isRecordPressed;
			if (isRecordPressed){
				recordBtn.setBackgroundResource(R.drawable.button_pressed);
				recordBtn.setImageResource(R.drawable.record_on);
				
				fHelper.startRecord(this, "Radio_" + System.currentTimeMillis());
			}else{
				recordBtn.setBackgroundResource(R.drawable.button_default);
				recordBtn.setImageResource(R.drawable.record_off);
				
				fHelper.stopRecord();
			}
			break;
		}
		
		//mixer button
		case R.id.button_mixer: {
			isMixerPressed = !isMixerPressed;
			if (isMixerPressed){
				mixerBtn.setBackgroundResource(R.drawable.button_pressed);
				mixerBtn.setImageResource(R.drawable.mixer_on);
			}else{
				mixerBtn.setBackgroundResource(R.drawable.button_default);
				mixerBtn.setImageResource(R.drawable.mixer_off);
			}
			break;
		}
		}
	}
//---------------------------------------------------------------------------
	/**
	 * Volume SeekBar listener
	 */
	@Override
	public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
		currentVolume = progress;
		if (! isMutePressed){
			audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {}
//---------------------------------------------------------------------------
	/**
	 * Starts service for playing music
	 */
	private void startPlayerService(){
		Intent service = new Intent(this, PlayerService.class);
		startService(service);
	}
//---------------------------------------------------------------------------
	/**
	 * Stops service for playing music
	 */
	private void stopPlayerService(){
		Intent service = new Intent(this, PlayerService.class);
		stopService(service);
	}
//---------------------------------------------------------------------------
	@Override
	protected void onDestroy() {
		super.onDestroy();
		stopPlayerService();	//not sure that is good idea - activity can die before service need to stop
	}
}